package com.mystore.servlet;

import com.mystore.entity.Kind;
import com.mystore.service.KindService;
import com.mystore.service.impl.KindServiceImpl;
import com.mystore.util.CartUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "KindServlet",value = "/check/kind/*")
public class KindServlet extends HttpServlet {
   KindService kindService = new KindServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String uri = request.getRequestURI();
       String method = uri.replace(request.getContextPath() + "/check/kind/", "");
       System.out.println("method="+method);
        PrintWriter out = response.getWriter();
        String url = "";
        switch (method){
            case "getAllKind"://获取所有的分类
                List<Kind> list = kindService.findAllKind();
                request.setAttribute("kind",list);
                url= "/WEB-INF/page/main.jsp";
                request.getRequestDispatcher(url).forward(request,response);
                break;
            case "loginout"://退出登录
                HttpSession session = request.getSession();
                try {
                    CartUtil.updateSession(session);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                session.removeAttribute(UserServlet.USER);
                System.out.println("退出成功");
                response.sendRedirect(request.getContextPath()+"/login.jsp");
                break;

            default:
                    out.println("invalid mark");

        }





    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
